package com.xmy.cultivate.service.impl;

import com.xmy.cultivate.constants.CommonConstant;
import com.xmy.cultivate.entity.Recruit;
import com.xmy.cultivate.entity.RecruitPushLog;
import com.xmy.cultivate.entity.Staff;
import com.xmy.cultivate.entity.views.MqCommonDataRs;
import com.xmy.cultivate.mapper.RecruitMapper;
import com.xmy.cultivate.respons.QyWeChartPushRs;
import com.xmy.cultivate.service.IQyWacherService;
import com.xmy.cultivate.service.IRecruitPushLogService;
import com.xmy.cultivate.service.IRecruitService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmy.cultivate.service.IStaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * <p>
 * 招聘信息 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2025-05-20
 */
@Service
public class RecruitServiceImpl extends ServiceImpl<RecruitMapper, Recruit> implements IRecruitService {
    @Autowired
    IRecruitService iRecruitService;
    @Autowired
    IStaffService iStaffService;

    @Autowired
    IQyWacherService iqyWacherService;

    @Autowired
    IRecruitPushLogService iRecruitPushLogService;


    @Override
    public void pushInterview(MqCommonDataRs mqCommonDataRs) {

        try {

            Long recruitId = mqCommonDataRs.getId();
            Recruit recruit = iRecruitService.getById(recruitId);
            if (recruit != null) {
                if (recruit.getIsArrive() == null) {

                    Staff staff = iStaffService.getById(recruit.getInviterId());
                    if (staff != null && staff.getQyWecharId() != null) {
                        String pushTitle = "面试提醒";
                        String content = "您有一场面试即将开始，请及时更新求职者最新状态,求职者：" + recruit.getName();

                        //推送内容
                        QyWeChartPushRs qyWeChartPushRs = new QyWeChartPushRs();
                        qyWeChartPushRs.setToUser(staff.getQyWecharId());
                        qyWeChartPushRs.setTitle(pushTitle);
                        qyWeChartPushRs.setContent(content);

                        qyWeChartPushRs.setRemark("请及时查看");
                        qyWeChartPushRs.setDateTime(LocalDateTime.now().withNano(0).toString().replace("T", " "));
                        qyWeChartPushRs.setLinkUrl(CommonConstant.teacherWebUrl);
                        //推送到企业微信
                        iqyWacherService.pushCardContent(qyWeChartPushRs);

                        RecruitPushLog log = new RecruitPushLog();
                        log.setRecruitId(recruitId);
                        log.setType(4); // 4面试提醒
                        log.setPushStaffId(recruit.getInviterId());
                        log.setStatus(1);  //1已推送
                        iRecruitPushLogService.save(log);
                    }
                } else {
                    RecruitPushLog log = new RecruitPushLog();
                    log.setRecruitId(recruitId);
                    log.setType(4); // 4面试提醒
                    log.setPushStaffId(recruit.getInviterId());
                    log.setStatus(0);  //1未推送
                    log.setRemark(recruit.getName()+"提前到");
                    iRecruitPushLogService.save(log);
                }
            }

        } catch (Exception e) {
            log.error("消息消费失败", e);
        }
    }

/*    @Override
    public void pushInterview(Map<String, Object> message) {

        try {
            Long recruitId = Long.valueOf(message.get("recruitId").toString());
            Recruit recruit = iRecruitService.getById(recruitId);
            if (recruit != null) {

                Staff staff = iStaffService.getById(recruit.getInviterId());
                if (staff != null && staff.getQyWecharId() != null) {
                    String pushTitle = "面试提醒";
                    String content = "您有一场面试即将开始，请及时更新求职者最新状态,求职者：" + recruit.getName();

                    //推送内容
                    QyWeChartPushRs qyWeChartPushRs = new QyWeChartPushRs();
                    qyWeChartPushRs.setToUser(staff.getQyWecharId());
                    qyWeChartPushRs.setTitle(pushTitle);
                    qyWeChartPushRs.setContent(content);

                    qyWeChartPushRs.setRemark("请及时查看");
                    qyWeChartPushRs.setDateTime(LocalDateTime.now().withNano(0).toString().replace("T", " "));
                    qyWeChartPushRs.setLinkUrl(CommonConstant.teacherWebUrl);
                    //推送到企业微信
                    iqyWacherService.pushCardContent(qyWeChartPushRs);

                    RecruitPushLog log = new RecruitPushLog();
                    log.setRecruitId(recruitId);
                    log.setType(4); // 4面试提醒
                    log.setPushStaffId(recruit.getInviterId());
                    log.setStatus(1);  //1已推送
                    iRecruitPushLogService.save(log);
                }
            }

        } catch (Exception e) {
            log.error("消息消费失败", e);
        }
    }*/
}
